<template>
  <ele-modal
    :width="550"
    :visible="visible"
    :confirm-loading="loading"
    :title="'编辑'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 5 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="24" :sm="24" :xs="24">
          <!-- <a-form-item label="门店logo" v-bind="validateInfos.logo">
            <a-upload
              name="attachment"
              v-model:file-list="images"
              :action="uploadUrl"
              :limit="1"
              list-type="picture-card"
              @change="uploadHandler"
              :show-upload-list="false"
            >
              <img
                v-if="imageUrl"
                :src="imageUrl"
                style="width: 100%"
                alt="example"
              />
              <div v-else>
                <loading-outlined v-if="loading" />
                <plus-outlined v-else />
                <div class="ant-upload-text">Upload</div>
              </div>
            </a-upload>
          </a-form-item> -->
          <a-form-item label="门店名称" v-bind="validateInfos.name">
            <a-input
              allow-clear
              placeholder="请输入门店名称"
              v-model:value="form.name"
              autocomplete="off"
              @blur="validate('name', { trigger: 'change' }).catch(() => {})"
            />
          </a-form-item>
          <a-form-item label="联系方式" v-bind="validateInfos.mobile">
            <a-input
              allow-clear
              placeholder="请输入联系方式"
              v-model:value="form.mobile"
              autocomplete="off"
              @blur="validate('mobile', { trigger: 'change' }).catch(() => {})"
            />
          </a-form-item>
          <a-form-item label="所在地区">
            <!-- <a-input
              allow-clear
              placeholder="请输入所在地区"
              v-model:value="form.region"
              autocomplete="new-password"
              @blur="
                validate('region', { trigger: 'change' }).catch(() => {})
              "
            /> -->
            <a-cascader
              :fieldNames="fieldNames"
              placeholder="请选择所在地区"
              :options="options"
              v-model:value="defaultValue"
              @change="changeRegion"
            />
          </a-form-item>
          <a-form-item label="详细地址">
            <a-input
              allow-clear
              placeholder="请输入详细地址"
              v-model:value="form.addr"
              autocomplete="new-password"
              @blur="validate('addr', { trigger: 'change' }).catch(() => {})"
            />
          </a-form-item>
          <!-- <a-form-item label="客服微信" v-bind="validateInfos.wechat">
            <a-input
              allow-clear
              placeholder="请输入客服微信"
              v-model:value="form.wechat"
              autocomplete="new-password"
              @blur="validate('wechat', { trigger: 'change' }).catch(() => {})"
            />
          </a-form-item> -->
          <a-form-item label="银行账号">
            <a-input
              allow-clear
              placeholder="请输入银行账号"
              v-model:value="form.account"
              autocomplete="new-password"
              @blur="validate('account', { trigger: 'change' }).catch(() => {})"
            />
          </a-form-item>
          <a-form-item label="开户行">
            <a-input
              allow-clear
              placeholder="请输入开户行"
              v-model:value="form.account_bank"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="开户名">
            <a-input
              allow-clear
              placeholder="请输入开户名"
              v-model:value="form.account_holder"
              autocomplete="off"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form } from 'ant-design-vue';
  import { phoneReg } from 'ele-admin-pro';
  import { PlusOutlined, LoadingOutlined } from '@ant-design/icons-vue';
  import { get_detail, get_address } from '@/api/tenant/store';
  // import { Item } from 'ant-design-vue/lib/menu';
  // import dayjs from 'dayjs';
  // 上传data数据
  // const uploadRespones = (file) => {
  //   console.log(file);
  // };
  // 表格列配置
  const useForm = Form.useForm;

  const emit = defineEmits(['done', 'update:visible']);

  // 上传接口
  const uploadUrl = ref(`http://zhubao.com/admin.php/system/basic/upload`);

  const imageUrl = ref('');

  const options = ref([]);
  const defaultValue = ref(null);

  // 地址选择下拉框更改label、balue
  const fieldNames = ref({
    label: 'label',
    value: 'value'
  });
  // 头像上传
  const images = ref([]);
  /* 完全自己控制选择文件后的处理 */
  const uploadHandler = (info) => {
    if (info && info.file && info.file.response) {
      const data = info.file.response.data;
      imageUrl.value = data.path;
      form.logo = data.id.toString();
    }
  };
  // eslint-disable-next-line no-unused-vars
  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 编辑数据id
    editDataId: [String, Number]
  });

  // 提交状态
  const loading = ref(false);

  // 表单数据
  const form = reactive({
    id: '',
    name: '',
    mobile: '',
    region: '',
    addr: '',
    account: '',
    account_bank: '',
    account_holder: ''
  });
  const changeRegion = (e) => {
    console.log('地区改变：', e);
    form.region = e[e.length - 1];
  };
  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        message: '请输入门店名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    mobile: [
      {
        required: true,
        message: '请输入联系方式',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);
  // resetFields
  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
  const getOptions = () => {
    get_address().then((res) => {
      options.value = res;
    });
  };
  onMounted(() => {
    getOptions();
    if (props.editDataId) {
      get_detail({ id: props.editDataId }).then((res) => {
        Object.keys(form).map((key) => {
          form[key] = res ? res[key] : '';
        });
        imageUrl.value = res.logo_path;
        form.region = res.region;
        defaultValue.value = res.region_name;
      });
    }
  });
</script>
<style scoped></style>
